package org.example.springboot.controller;

import org.example.springboot.common.Result;
import org.example.springboot.entity.Order;
import org.example.springboot.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/page")
    public Result getOrderPage(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String teachername,
            @RequestParam int pageNum,
            @RequestParam int pageSize) {

        List<Order> orders;
        int totalCount;

        if (name != null && teachername != null) {
            // 根据家长姓名和教师姓名进行分页查询
            orders = orderService.getOrdersByParentAndTeacher(name, teachername, pageNum, pageSize);
            totalCount = orderService.countOrdersByParentAndTeacher(name, teachername);
        } else if (name != null) {
            // 仅根据家长姓名进行分页查询
            orders = orderService.getOrdersByParent(name, pageNum, pageSize);
            totalCount = orderService.countOrdersByParent(name);
        } else if (teachername != null) {
            // 仅根据教师姓名进行分页查询
            orders = orderService.getOrdersByTeacher(teachername, pageNum, pageSize);
            totalCount = orderService.countOrdersByTeacher(teachername);
        } else {
            return Result.error("查询条件不能为空");
        }

        // 构建返回数据
        Map<String, Object> response = new HashMap<>();
        response.put("list", orders);
        response.put("total", totalCount);

        return Result.success(response);
    }

    @PostMapping("/create")
    public Result createOrder(@RequestBody Order order) {
        Order o = orderService.getOrderByAll(order.getUser_account(),order.getTeacher_account(), order.getCourse());

        if (o != null) {
            return Result.error("请勿重复预约");
        }
        orderService.insertOrder(order);
        return Result.success();
    }

    @GetMapping("/selectByTeacher")
    public Result selectAll(@RequestParam String teacher_account) {
        return Result.success(orderService.findAllOrders(teacher_account));
    }

    @GetMapping("/selectByUser")
    public Result selectAllByUser(@RequestParam String user_account) {
        return Result.success(orderService.findAllOrdersByUser(user_account));
    }

    @PostMapping("/confirm")
    public Result confirmOrder(@RequestParam int order_id) {
        orderService.updateOrderStatus(order_id);
        return Result.success();
    }

    @PostMapping("/cancel")
    public Result cancelOrder(@RequestParam int order_id) {
        orderService.updateOrderDuration(order_id);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delOrder(@PathVariable int id) {
        orderService.deleteOrder(id);
        return Result.success();
    }

    @GetMapping("/selectById")
    public Result selectOrder(@RequestParam int id) {
        return Result.success(orderService.selectOrder(id));
    }
}
